# frozen_string_literal: true

# config.encoding = "utf-8"
require 'csv'
require 'active_record'
require 'pp'
require 'mysql2'
require 'uuidtools'

file_name = './ex_emp.csv'

# Time.zone_default = Time.find_zone! 'Tokyo'
ActiveRecord::Base.default_timezone = :local

ActiveRecord::Base.configurations = YAML.load_file('./database.yml')
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['development'])


class DimensionTree < ActiveRecord::Base
  self.table_name = 'ep_f_dimension_tree'
end

# insert

# User.delete_all

# EpFUnit.create(id: new_uuid, unit_code: 'tanaka1', name: 19, unit_type_code: '1')
header = %w[STATUS,USERID,USERNAME,FIRSTNAME,NICKNAME,LASTNAME,TITLE,GENDER,EMAIL,MANAGER,HR,DEPARTMENT,JOBCODE,DIVISION,LOCATION,TIMEZONE,HIREDATE,EMPID,BIZ_PHONE,CELL_PHONE,FAX,ADDR1,ADDR2,CITY,STATE,ZIP,COUNTRY,REVIEW_FREQ,LAST_REVIEW_DATE,MATRIX_MANAGER,DEFAULT_LOCALE,PROXY,seatingChart,CUSTOM01,CUSTOM02,CUSTOM03,CUSTOM04,CUSTOM05,CUSTOM06,CUSTOM07,CUSTOM08,CUSTOM09,CUSTOM10,CUSTOM11,CUSTOM12,CUSTOM13,CUSTOM14,CUSTOM15,CUSTOM_MANAGER,SECOND_MANAGER,LOGIN_METHOD,JOBTITLE]
cols = %w[STATUS USERID 用户名 姓名 昵称 姓氏 职位 性别 电子邮件 上级主管 人力资源 部门 职称 部门 地点 时区 录用日期 工号 办公电话 手机]
# pp cols[1]
# pp cols['用户名']

# message = '区域拓展(50000024)'.gsub(/\s+/, '')

# regex.match(message)
# pp Regexp.last_match(1)
# pp Regexp.last_match(2)
def input_data
  file_name = './ex_emp.csv'
  regex = /(\p{Han}*)\((\d*)\)/
  CSV.foreach(file_name, encoding: 'utf-8', headers: true) do |row|
    pp row['DIVISION']
    division = row['DIVISION'].gsub(/\s+/, '')
    regex.match(division)
    name = Regexp.last_match(1)
    unit_code = Regexp.last_match(2)
    # if unit_code
    #   unless EpFUnit.find_by(unit_code: unit_code)
    #     new_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
    #     EpFUnit.create(id: new_uuid, unit_type_code: '1',
    #                    unit_code: unit_code,
    #                    name: name)
    #   end
    # end

    work_no = row['USERID'].gsub(/\s+/, '')
    real_name = row['FIRSTNAME']
    if work_no
      new_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
      # parent_id_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
      if DimensionTree.find(:all,:conditions => ["node_code = ? and dimension_code = '0001'", work_no]) 
        DimensionTree.delete(:all,:conditions => ["node_code = ? and dimension_code = '0001'", work_no])
      end
      DimensionTree.create(id: new_uuid,
                           #  parent_id: parent_id_uuid,
                           dimension_code: '0002',
                           node_category: 3,
                           node_code: work_no)
    end
  end
end

def update_parent
  file_name = './ex_emp.csv'
  CSV.foreach(file_name, encoding: 'utf-8', headers: true) do |row|
    manager_id = row['HR']
    pp manager_id
    work_no = row['USERID'].gsub(/\s+/, '')
    pp work_no
    user = DimensionTree.find_by(node_code: work_no,dimension_code:'0002')
    if manager_id == 'NO_HR'
      user&.update(parent_id: 0)
    else
      manager = DimensionTree.find_by(node_code: manager_id,dimension_code:'0002')
      user&.update(parent_id: manager.id)
    end
  end
end

def input_tree_data
  file_name = './ex_emp.csv'
  regex = /(\p{Han}*)\((\d*)\)/
  CSV.foreach(file_name, encoding: 'utf-8', headers: true) do |row|
    # puts row['DIVISION']
    division = row['DIVISION'].gsub(/\s+/, '')
    regex.match(division)
    name = Regexp.last_match(1)
    unit_code = Regexp.last_match(2)

    work_no = row['USERID'].gsub(/\s+/, '')
    real_name = row['FIRSTNAME']
    if work_no
      new_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
      # parent_id_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
      if DimensionTree.find_by(node_code: work_no,dimension_code:'0002')
        DimensionTree.delete_by(node_code: work_no,dimension_code:'0002')
      end
      DimensionTree.create(id: new_uuid,
                           #  parent_id: parent_id_uuid,
                           dimension_code: '0002',
                           node_category: 3,
                           node_code: work_no)
    end
  end
  CSV.foreach(file_name, encoding: 'utf-8', headers: true) do |row|
    # puts row['DIVISION']
    division = row['DIVISION'].gsub(/\s+/, '')
    regex.match(division)
    name = Regexp.last_match(1)
    unit_code = Regexp.last_match(2)

    work_no = row['USERID'].gsub(/\s+/, '')
    real_name = row['FIRSTNAME']
    if work_no
      new_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
      # parent_id_uuid = UUIDTools::UUID.timestamp_create.to_s.gsub('-', '')
      if DimensionTree.find_by(node_code: work_no,dimension_code:'0002')
        DimensionTree.delete_by(node_code: work_no,dimension_code:'0002')
      end
      DimensionTree.create(id: new_uuid,
                           #  parent_id: parent_id_uuid,
                           dimension_code: '0002',
                           node_category: 3,
                           node_code: work_no)
    end
  end
end

#HR只用找一层即可
def find_parent(p_parent_id, brd)
  node = DimensionTree.find_by(id: p_parent_id,dimension_code:'0002')
  parent_id = node.parent_id
  new_brd = node.node_code + ',' + brd
#   if parent_id != '0'
#     find_parent(parent_id, new_brd)
#   else
#     new_brd
#   end
end

def build_breadcrumb
  DimensionTree.where(dimension_code:'0002').find_each do |node|
    parent_id = node.parent_id
    if parent_id != '0'
      brd = find_parent(parent_id, node.node_code)
      node.breadcrumb = brd
    else
      node.breadcrumb = node.node_code
    end
    node.save
  end
end

def build_tree
  input_tree_data
  update_parent
  build_breadcrumb
end

# input_tree_data
# update_parent
build_breadcrumb